home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / xpcom / nsCRT.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  10KB  |  285 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsCRT_h___
  38. #define nsCRT_h___
  39.  
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include "plstr.h"
  44. #include "nscore.h"
  45. #include "prtypes.h"
  46. #include "nsCppSharedAllocator.h"
  47.  
  48. #if defined(XP_WIN) || defined(XP_OS2)
  49. #  define NS_LINEBREAK           "\015\012"
  50. #  define NS_LINEBREAK_LEN       2
  51. #else
  52. #  if defined(XP_UNIX) || defined(XP_BEOS)
  53. #    define NS_LINEBREAK         "\012"
  54. #    define NS_LINEBREAK_LEN     1
  55. #  endif /* XP_UNIX */
  56. #endif /* XP_WIN || XP_OS2 */
  57.  
  58. extern const PRUnichar kIsoLatin1ToUCS2[256];
  59.  
  60. // This macro can be used in a class declaration for classes that want
  61. // to ensure that their instance memory is zeroed.
  62. #define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW   \
  63.   void* operator new(size_t sz) CPP_THROW_NEW { \
  64.     void* rv = ::operator new(sz);              \
  65.     if (rv) {                                   \
  66.       memset(rv, 0, sz);                        \
  67.     }                                           \
  68.     return rv;                                  \
  69.   }                                             \
  70.   void operator delete(void* ptr) {             \
  71.     ::operator delete(ptr);                     \
  72.   }
  73.  
  74. // This macro works with the next macro to declare a non-inlined
  75. // version of the above.
  76. #define NS_DECL_ZEROING_OPERATOR_NEW           \
  77.   void* operator new(size_t sz) CPP_THROW_NEW; \
  78.   void operator delete(void* ptr);
  79.  
  80. #define NS_IMPL_ZEROING_OPERATOR_NEW(_class)            \
  81.   void* _class::operator new(size_t sz) CPP_THROW_NEW { \
  82.     void* rv = ::operator new(sz);                      \
  83.     if (rv) {                                           \
  84.       memset(rv, 0, sz);                                \
  85.     }                                                   \
  86.     return rv;                                          \
  87.   }                                                     \
  88.   void _class::operator delete(void* ptr) {             \
  89.     ::operator delete(ptr);                             \
  90.   }
  91.  
  92. // Freeing helper
  93. #define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
  94.  
  95. /// This is a wrapper class around all the C runtime functions. 
  96.  
  97. class NS_COM nsCRT {
  98. public:
  99.   enum {
  100.     TAB='\t'  /* Horizontal Tab */,
  101.     LF='\n'   /* Line Feed */,
  102.     VTAB='\v' /* Vertical Tab */,
  103.     FF='\f'   /* Form Feed */,
  104.     CR='\r'   /* Carriage Return */
  105.   };
  106.  
  107.   /***
  108.    ***  The following nsCRT::mem* functions are no longer
  109.    ***  supported, please use the corresponding lib C
  110.    ***  functions instead.
  111.    ***
  112.    ***  nsCRT::memcpy()
  113.    ***  nsCRT::memcmp()
  114.    ***  nsCRT::memmove()
  115.    ***  nsCRT::memset()
  116.    ***  nsCRT::zero()
  117.    ***
  118.    ***  Additionally, the following char* string utilities
  119.    ***  are no longer supported, please use the
  120.    ***  corresponding lib C functions instead.
  121.    ***
  122.    ***  nsCRT::strlen()
  123.    ***
  124.    ***/
  125.  
  126.   /** Compute the string length of s
  127.    @param s the string in question
  128.    @return the length of s
  129.    */
  130.   static PRUint32 strlen(const char* s) {                                       
  131.     return PRUint32(::strlen(s));                                               
  132.   }                                                                             
  133.  
  134.   /// Compare s1 and s2.
  135.   static PRInt32 strcmp(const char* s1, const char* s2) {
  136.     return PRInt32(PL_strcmp(s1, s2));
  137.   }
  138.  
  139.   static PRInt32 strncmp(const char* s1, const char* s2,
  140.                          PRUint32 aMaxLen) {
  141.     return PRInt32(PL_strncmp(s1, s2, aMaxLen));
  142.   }
  143.  
  144.   /// Case-insensitive string comparison.
  145.   static PRInt32 strcasecmp(const char* s1, const char* s2) {
  146.     return PRInt32(PL_strcasecmp(s1, s2));
  147.   }
  148.  
  149.   /// Case-insensitive string comparison with length
  150.   static PRInt32 strncasecmp(const char* s1, const char* s2, PRUint32 aMaxLen) {
  151.     PRInt32 result=PRInt32(PL_strncasecmp(s1, s2, aMaxLen));
  152.     //Egads. PL_strncasecmp is returning *very* negative numbers.
  153.     //Some folks expect -1,0,1, so let's temper its enthusiasm.
  154.     if (result<0) 
  155.       result=-1;
  156.     return result;
  157.   }
  158.  
  159.   static PRInt32 strncmp(const char* s1, const char* s2, PRInt32 aMaxLen) {
  160.     // inline the first test (assumes strings are not null):
  161.     PRInt32 diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
  162.     if (diff != 0) return diff;
  163.     return PRInt32(PL_strncmp(s1,s2,unsigned(aMaxLen)));
  164.   }
  165.   
  166.   static char* strdup(const char* str) {
  167.     return PL_strdup(str);
  168.   }
  169.  
  170.   static char* strndup(const char* str, PRUint32 len) {
  171.     return PL_strndup(str, len);
  172.   }
  173.  
  174.   static void free(char* str) {
  175.     PL_strfree(str);
  176.   }
  177.  
  178.   /**
  179.  
  180.     How to use this fancy (thread-safe) version of strtok: 
  181.  
  182.     void main(void) {
  183.       printf("%s\n\nTokens:\n", string);
  184.       // Establish string and get the first token:
  185.       char* newStr;
  186.       token = nsCRT::strtok(string, seps, &newStr);   
  187.       while (token != NULL) {
  188.         // While there are tokens in "string"
  189.         printf(" %s\n", token);
  190.         // Get next token:
  191.         token = nsCRT::strtok(newStr, seps, &newStr);
  192.       }
  193.     }
  194.     * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
  195.     * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
  196.   */
  197.   static char* strtok(char* str, const char* delims, char* *newStr); 
  198.  
  199.   /// Like strlen except for ucs2 strings
  200.   static PRUint32 strlen(const PRUnichar* s);
  201.  
  202.   /// Like strcmp except for ucs2 strings
  203.   static PRInt32 strcmp(const PRUnichar* s1, const PRUnichar* s2);
  204.   /// Like strcmp except for ucs2 strings
  205.   static PRInt32 strncmp(const PRUnichar* s1, const PRUnichar* s2,
  206.                          PRUint32 aMaxLen);
  207.  
  208.   // You must use nsCRT::free(PRUnichar*) to free memory allocated
  209.   // by nsCRT::strdup(PRUnichar*).
  210.   static PRUnichar* strdup(const PRUnichar* str);
  211.  
  212.   // You must use nsCRT::free(PRUnichar*) to free memory allocated
  213.   // by strndup(PRUnichar*, PRUint32).
  214.   static PRUnichar* strndup(const PRUnichar* str, PRUint32 len);
  215.  
  216.   static void free(PRUnichar* str) {
  217.       nsCppSharedAllocator<PRUnichar> shared_allocator;
  218.       shared_allocator.deallocate(str, 0 /*we never new or kept the size*/);
  219.   }
  220.  
  221.   // Computes the hashcode for a c-string, returns the string length as
  222.   // an added bonus.
  223.   static PRUint32 HashCode(const char* str,
  224.                            PRUint32* resultingStrLen = nsnull);
  225.  
  226.   // Computes the hashcode for a ucs2 string, returns the string length
  227.   // as an added bonus.
  228.   static PRUint32 HashCode(const PRUnichar* str,
  229.                            PRUint32* resultingStrLen = nsnull);
  230.  
  231.   // Computes the hashcode for a buffer with a specified length.
  232.   static PRUint32 BufferHashCode(const PRUnichar* str, PRUint32 strLen);
  233.  
  234.   // String to longlong
  235.   static PRInt64 atoll(const char *str);
  236.   
  237.   static char ToUpper(char aChar);
  238.  
  239.   static char ToLower(char aChar);
  240.   
  241.   static PRBool IsUpper(char aChar);
  242.  
  243.   static PRBool IsLower(char aChar);
  244.  
  245.   static PRBool IsAscii(PRUnichar aChar);
  246.   static PRBool IsAscii(const PRUnichar* aString);
  247.   static PRBool IsAsciiAlpha(PRUnichar aChar);
  248.   static PRBool IsAsciiDigit(PRUnichar aChar);
  249.   static PRBool IsAsciiSpace(PRUnichar aChar);
  250.   static PRBool IsAscii(const char* aString);
  251.   static PRBool IsAscii(const char* aString, PRUint32 aLength);
  252. };
  253.  
  254. #define FF '\014'
  255. #define TAB '\011'
  256.  
  257. #define CRSTR "\015"
  258. #define LFSTR "\012"
  259. #define CRLF "\015\012"     /* A CR LF equivalent string */
  260.  
  261.  
  262. #if defined(XP_WIN) || defined(XP_OS2)
  263.   #define FILE_PATH_SEPARATOR       "\\"
  264.   #define FILE_ILLEGAL_CHARACTERS   "/:*?\"<>|"
  265. #elif defined(XP_UNIX) || defined(XP_BEOS)
  266.   #define FILE_PATH_SEPARATOR       "/"
  267.   #define FILE_ILLEGAL_CHARACTERS   ""
  268. #else
  269.   #error need_to_define_your_file_path_separator_and_illegal_characters
  270. #endif
  271.  
  272. #define NS_IS_SPACE(VAL) \
  273.   (((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
  274.  
  275. #define NS_IS_CNTRL(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
  276. #define NS_IS_DIGIT(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
  277. #if defined(XP_WIN) || defined(XP_OS2)
  278. #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
  279. #else
  280. #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
  281. #endif
  282.  
  283.  
  284. #endif /* nsCRT_h___ */
  285.